﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightAnimation
{
    public class OpacityAnimation : AnimationBase
    {
        public OpacityAnimation(FrameworkElement element, double opacity, int seconds)
            : base(element, new TimeSpan(0, 0, seconds))
        {
            Opactity = opacity;
        }
        public double Opactity
        {
            get;
            private set;
        }
        protected override Storyboard CreateStoryboard()
        {
            var storyboard = new Storyboard();
            var opacityAnimation = new DoubleAnimation();
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(FrameworkElement.Opacity)"));
            storyboard.Children.Add(opacityAnimation);
            return storyboard;
        }
        protected override void ApplyValues(Storyboard storyboard)
        {
            if (storyboard == null)
                throw new ArgumentNullException("storyboard");
            var opacityAnimation = storyboard.Children[0] as DoubleAnimation;
            opacityAnimation.To = Opactity;
            opacityAnimation.Duration = AnimationDuration;
        }
        public static void FadeOut(FrameworkElement element, int seconds)
        {
            new OpacityAnimation(element, 0, seconds).Apply();
        }
        public static void FadeIn(FrameworkElement element, int seconds)
        {
            new OpacityAnimation(element, 1.0, seconds).Apply();
        }
        public static void Fade(FrameworkElement element, double opacity, int seconds)
        {
            new OpacityAnimation(element, opacity, seconds).Apply();
        }
        public static void CrossFade(FrameworkElement elementToFadeOut, FrameworkElement elementToFadeIn, int seconds)
        {
            FadeOut(elementToFadeOut, seconds);
            FadeIn(elementToFadeIn, seconds);
        }
    }

}
